﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_basisset_UserDetail : AdminPageBase
{
   int id;
   string spwd;
   protected string groupName;
   protected string username;
   private string returnURL;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
       // GetUrlArgs();
        GetUserPermisions();
        GetUserId();
        if (Request.QueryString["returnURL"]!=null)
        returnURL = Request.QueryString["returnURL"].ToString();
        if (!IsPostBack)
        {
            BindGroup();
            GetUserInfo();
            BindUserPermision(); 
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void BindGroup()
    {
        DataSet ds = jtDataAccess.GetList("[Group]","1=1","GroupId");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlgroup.DataSource = ds;
            ddlgroup.DataTextField = "Name";
            ddlgroup.DataValueField = "GroupId";
            ddlgroup.DataBind();

        }
        ListItem item=new ListItem("=请选择组别=","0");
        ddlgroup.Items.Insert(0,item);
    }
    protected void GetUserInfo()
    {
        if (id != 0)
        {
            DataSet ds = new DataSet();
            ds = jtDataAccess.GetUserInfo(id);
            if (CoreTools.CheckDataSet(ds))
            {
                txtname.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                txtcode.Text=ds.Tables[0].Rows[0]["Code"].ToString();
                ddlgroup.SelectedValue=ds.Tables[0].Rows[0]["GroupId"].ToString();
                rbtlIsvalid.SelectedValue=ds.Tables[0].Rows[0]["IsValid"].ToString();
                spwd = ds.Tables[0].Rows[0]["PWD"].ToString();
                groupName = ds.Tables[0].Rows[0]["GroupName"].ToString();
                username = ds.Tables[0].Rows[0]["Name"].ToString();
              
                if (groupName == "Administrators" && username== "Admin")
                {
                    grdvPermision.Visible = false;
                    txtcode.Enabled = false;
                    txtname.Enabled = false;
                    rbtlIsvalid.Enabled = false;
                    ddlgroup.Enabled = false;
                   
                }
                else
                {
                    grdvPermision.Visible = true;
                    txtcode.Enabled = true;
                    txtname.Enabled = true;
                    rbtlIsvalid.Enabled = true;
                    ddlgroup.Enabled = true ;
                    
                }

            }
           
        }

    }

    protected void GetUserId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;
            lblmsg1.Visible = false;
            lblmsg2.Visible = false;
          
        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
            lblmsg1.Visible = true;
            lblmsg2.Visible = true;
            hdfuserid.Value = id.ToString();

        }
    }

    protected void BindUserPermision()
    {
       // if (id != 0)
        {
           
            DataTable ds = jtDataAccess.GetUserPermision(id);
            if (ds!=null&&ds.Rows.Count>0)
            {

                grdvPermision.DataSource = ds;
                grdvPermision.DataBind();
            }

        }
    }
      
    protected void grdvPermision_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdvPermision_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataSet ds = new DataSet();
        ds = jtDataAccess.GetList("Permision", "1=1", "PermisionId");
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            CheckBoxList ckblp = (CheckBoxList)e.Row.FindControl("ckblpermision");
            CheckBox ckball = (CheckBox)e.Row.FindControl("ckall");
            HiddenField hdfMenuId = (HiddenField)e.Row.FindControl("hdfMenuId");
            HiddenField hdfPermisionNew = (HiddenField)e.Row.FindControl("hdfPermisionNew");
            HiddenField hdfPermisionSelect = (HiddenField)e.Row.FindControl("hdfPermisionSelect");
            HiddenField hdfPermisionEdit = (HiddenField)e.Row.FindControl("hdfPermisionEdit");
            HiddenField hdfPermisionDelete = (HiddenField)e.Row.FindControl("hdfPermisionDelete");
            if (ckball != null)
            {
                if (edit == 1)
                    ckball.Enabled = true;
                else
                    ckball.Enabled = false;
            }
            if (ckblp != null)
            {
                if(edit==1){
                    ckblp.Enabled = true;
                    if (CoreTools.CheckDataSet(ds))
                    {
                        ckblp.DataSource = ds;
                        ckblp.DataTextField = "Name";
                        ckblp.DataValueField = "PermisionId";
                        ckblp.DataBind();

                        int count = ds.Tables[0].Rows.Count;
                        if (hdfMenuId != null)
                            for (int j = 0; j < count; j++)
                            {
                                if (id == 1)
                                    ckblp.Items[j].Selected = true;
                                else
                                {
                                    ckblp.Items[j].Selected = false;
                                    string permisinName = ckblp.Items[j].Text;
                                    switch (permisinName)
                                    {
                                        case "新增":
                                            if (hdfPermisionNew != null && hdfPermisionNew.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;
                                        case "修改":
                                            if (hdfPermisionEdit != null && hdfPermisionEdit.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;
                                        case "查看":
                                            if (hdfPermisionSelect != null && hdfPermisionSelect.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;
                                        case "删除":
                                            if (hdfPermisionDelete != null && hdfPermisionDelete.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;

                                    }
                                }
                            }
                    }
                    else
                    {
                        ckblp.Enabled = false;
                    }
                }

            }

        }
        
    }
    protected void ckall_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;
        GridViewRow gvr = (GridViewRow)dcf.Parent;
        CheckBoxList ckblp = (CheckBoxList)dcf.FindControl("ckblpermision");
        try
        {
            if (chk.Checked)
            {
               
                if (ckblp != null)
                {
                    for (int i = 0; i < ckblp.Items.Count; i++)
                    {
                        ckblp.Items[i].Selected = true;
                    }
                }
            }
            else
            {

                if (ckblp != null)
                {
                    for (int i = 0; i < ckblp.Items.Count; i++)
                    {
                        ckblp.Items[i].Selected=false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //throw(this, ex.ToString());
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string name = txtname.Text.Trim();
        string code = txtcode.Text.Trim();
        if (!CoreTools.ProcessSqlStr(name, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtname.Focus();
        }
        else if(!CoreTools.ProcessSqlStr(code, 0))
        {
             ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
             txtcode.Focus();
        }
        else
        {
            int groupid = int.Parse(ddlgroup.SelectedValue.ToString());
            int isvalid = int.Parse(rbtlIsvalid.SelectedValue.ToString());
            string pwd = txtpwd.Text.Trim();
            UserType ut = new UserType();//用户自定义类型
            DataTable UserPermisionTable = ut.GetUserPermisionTableType();
            DataRow UserPermisionRow;
            JTDataAccess.OpType OpType;
            if (id == 0)
            {
                OpType = JTDataAccess.OpType.Insert;
                pwd = CryptorEngine.GetMd5_32(pwd);
            }
            else
            {
                OpType = JTDataAccess.OpType.Update;
                if (pwd == "")
                {
                    GetUserInfo();
                    pwd = spwd;
                }
                else
                    pwd = CryptorEngine.GetMd5_32(pwd);
            }
            for (int i = 0; i < grdvPermision.Rows.Count; i++)
            {
                CheckBoxList ckblp = (CheckBoxList)grdvPermision.Rows[i].FindControl("ckblpermision");
                HiddenField hdfMenuId = (HiddenField)grdvPermision.Rows[i].FindControl("hdfMenuId");

                if (ckblp != null && hdfMenuId != null)
                {
                    for (int j = 0; j < ckblp.Items.Count; j++)
                    {

                        if (ckblp.Items[j].Selected == true)
                        {
                            UserPermisionRow = UserPermisionTable.NewRow();
                            UserPermisionRow["MenuId"] = hdfMenuId.Value;
                            UserPermisionRow["PermisionId"] = ckblp.Items[j].Value;
                            UserPermisionTable.Rows.Add(UserPermisionRow);
                        }
                    }
                }

            }
            UserPermisionTable.AcceptChanges();
            int flag = jtDataAccess.OpUser(OpType, id, code, name, pwd, isvalid, groupid, UserPermisionTable);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，该帐号已经使用，请更换一个帐号！"), false);
                    break;
                case 0:

                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                    break;
                default:
                    GetUserInfo();
                    BindUserPermision();
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    //JsHelper.Alert("操作成功");
                    break;
            }
        }

    }
    protected void btnreturn_Click(object sender, EventArgs e)
    {
       // Response.Redirect("User.aspx?MenuId=" + MenuId);
        Response.Redirect(returnURL);
    }
    protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= grdvPermision.Rows.Count - 1; i++)
        {
            CheckBoxList ckblp = (CheckBoxList)grdvPermision.Rows[i].FindControl("ckblpermision");
            if (chkSelect.Checked == true)
            {
                if (ckblp != null)
                {
                    for (int j = 0; j < ckblp.Items.Count; j++)
                    {
                        ckblp.Items[j].Selected = true;
                    }
                }
            }
            else
            {
                if (ckblp != null)
                {
                    for (int j = 0; j < ckblp.Items.Count; j++)
                    {
                        ckblp.Items[j].Selected = false;
                    }
                }
            }
        }
    }
}